package com.haredot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haredot.dto.UserAdminDTO;
import com.haredot.entity.Organization;
import com.haredot.exception.ServiceAccessException;
import com.haredot.mapper.OrganizationMapper;
import com.haredot.service.OrganizationService;
import com.haredot.vo.PageVo;
import com.haredot.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrganizationServiceImpl implements OrganizationService {

    @Resource
    private OrganizationMapper organizationMapper ;

    @Override
    public R<?> saveOrganization(Organization organization) {
        // 根据组织机构名字查询是否重复
        Organization org = organizationMapper.findOrgByName(organization.getName());
        if (org != null) return R.fail(R.StatusCode.BAD).data(Map.of("name", "组织机构名已经存在"));

        // 根据 编码查询
        org = organizationMapper.findOrgByCode(organization.getCode()) ;
        if (org != null) return R.fail(R.StatusCode.BAD).data(Map.of("code", "组织机构编码已经存在"));

        // 保存组织机构
        int len  = organizationMapper.insert(organization) ;

        if (len != 1) throw new ServiceAccessException("插入组织机构失败、期待影响1行、实际影响" + len + "行");

        return R.ok().build();
    }

    @Override
    public R<?> updateOrganization(Organization organization) {
        // 根据组织机构名字查询是否重复
        Organization org = organizationMapper.findOrgByName(organization.getName());
        if (org != null && !Objects.equals(org.getId(), organization.getId())) {
            return R.fail(R.StatusCode.BAD).data(Map.of("name", "组织机构名已经存在"));
        }

        // 根据 编码查询
        org = organizationMapper.findOrgByCode(organization.getCode()) ;
        if (org != null && !Objects.equals(org.getId(), organization.getId())) {
            return R.fail(R.StatusCode.BAD).data(Map.of("code", "组织机构编码已经存在"));
        }

        if (organization.getPid() != null) {
            // 如果要修改的状态是 true , 则判断 父机构是否是 true
            org = organizationMapper.findOrgById(organization.getPid()) ;

            if (!org.getState() && organization.getState()) {
                return R.fail("更新数据失败").data(Map.of("state", "父机构的状态为禁用、不能更新本机构的状态为启用"));
            }
        }
        // 保存组织机构
        int len  = organizationMapper.update(organization) ;

        // 判断 修改的组织机构的状态 是否是 false, 如果 是 false , 则 禁用所有的子机构
        if (!organization.getState()) {
            // 将 该机构 及其 子机构 全部 设置为 禁用
            organizationMapper.updateChildrenStateForbidden(organization.getId()) ;
        }

        if (len != 1) throw new ServiceAccessException("插入组织机构失败、期待影响1行、实际影响" + len + "行");

        return R.ok().build();
    }

    @Override
    public R<Organization> queryOrganizationList() {

        List<Organization> list = organizationMapper.findAll();
        return R.<Organization>ok().data(list);
    }

    @Override
    public R<?> removeOrg(Long id) {
        int len = organizationMapper.deleteSelfAndChildrenOrgBySelfId(id);
        if (len  == 0) throw new ServiceAccessException("删除失败、期待删除至少一条记录、实际删除 0条") ;
        return R.ok().build();
    }

    @Override
    public R<PageVo> queryUserList(PageVo pageVo, Long orgId) {
        PageInfo<UserAdminDTO> info = PageHelper.startPage(pageVo)
                .doSelectPageInfo(() -> organizationMapper.findUsersByOrgId(orgId));

        pageVo.setTotal(info.getTotal()).setList(info.getList());

        return R.<PageVo>ok().data(pageVo);
    }
}
